
package fr.epsi.activities.employe;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.example.MyLittleHotel.R;
import fr.epsi.BDD.BDD;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Florent Baudon on 15/05/2014.
 */
public class AjouterEmployeActivity extends Activity implements View.OnClickListener {

    private EditText nom, prenom, contrat;
    private Spinner service;
    private String serviceSelect;
    private Button ajouter;
    private int temps = 35;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajouteremployelayout);

        //récupération des textview
        nom = (EditText)findViewById(R.id.nomEmp);
        prenom = (EditText)findViewById(R.id.prenomEmp);
        contrat = (EditText)findViewById(R.id.contratEmp);

        //Ajout et config du bouton
        ajouter = (Button)findViewById(R.id.ajouterEmp);
        ajouter.setOnClickListener(this);

        //Ajout et config du spinner
        service = (Spinner)findViewById(R.id.serviceEmp);
        List<String> servicesList = setListService();
        ArrayAdapter<String> spinnerAdapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, servicesList);
        spinnerAdapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        service.setAdapter(spinnerAdapt);
        service.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                serviceSelect = (String)adapterView.getItemAtPosition(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        if(!(nom.getText().toString()).equals("") && !(prenom.getText().toString()).equals("") && !(contrat.getText().toString()).equals("") && !serviceSelect.equals("")) {
            BDD bdd = new BDD();
            try {
                bdd.open(this);
                bdd.insertEmploye(nom.getText().toString(), prenom.getText().toString(), contrat.getText().toString(), serviceSelect, temps);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //vidage des champs texte
            nom.setText("");
            prenom.setText("");
            contrat.setText("");

            Toast t = Toast.makeText(this, "Employé ajouté", Toast.LENGTH_SHORT);
            t.show();
        }else{
            Toast t = Toast.makeText(this, "Veuillez remplir tout les champs", Toast.LENGTH_SHORT);
            t.show();
        }
    }
    //récupére la liste des service pour le spinner
    private List<String> setListService(){

        BDD bdd = new BDD();
        Cursor cListServices;
        try{
            bdd.open(this);
        }catch (SQLException e){
            e.printStackTrace();
        }
        cListServices = bdd.getServices();

        List<String> serviceList = new ArrayList<String>();

        if(cListServices!=null && cListServices.moveToFirst()){
            boolean finish=false;
            while(!finish) {
                serviceList.add(cListServices.getString(1));
                if (cListServices.isLast())
                    finish = true;
                else
                    cListServices.moveToNext();
            }
        }
        return serviceList;
    }
}